package glen.mgbean;

import glen.security.dao.RightDAO;
import glen.security.dto.Right;
import org.primefaces.component.datatable.DataTable;
import org.primefaces.context.RequestContext;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import java.io.Serializable;
import java.util.List;

public class RightMgb implements Serializable {

    /**
     *
     */
    private static final long serialVersionUID = 2389720517637080006L;

    private Integer rightIdSearch;
    private String rightNameSearch;
    private Integer activeSearch;
    private List<Right> resultSearch;
    private Right selected;
    private DataTable dtResult;
    RightDAO dao = new RightDAO(Right.class);

    public RightMgb() {
        super();
        this.reset();
    }

    public DataTable getDtResult() {
        return dtResult;
    }

    public void setDtResult(DataTable dtResult) {
        this.dtResult = dtResult;
    }

    @Override
    public String toString() {
        return "RightMgb [rightIdSearch=" + rightIdSearch + ", rightNameSearch="
                + rightNameSearch + ", activeSearch=" + activeSearch
                + ", resultSearch=" + resultSearch + ", selected=" + selected
                + "]";
    }

    public Integer getRightIdSearch() {
        return rightIdSearch;
    }

    public void setRightIdSearch(Integer rightIdSearch) {
        this.rightIdSearch = rightIdSearch;
    }

    public String getRightNameSearch() {
        return rightNameSearch;
    }

    public void setRightNameSearch(String rightNameSearch) {
        this.rightNameSearch = rightNameSearch;
    }

    public Integer getActiveSearch() {
        return activeSearch;
    }

    public void setActiveSearch(Integer activeSearch) {
        this.activeSearch = activeSearch;
    }

    public List<Right> getResultSearch() {
        return resultSearch;
    }

    public void setResultSearch(List<Right> resultSearch) {
        this.resultSearch = resultSearch;
    }

    public Right getSelected() {
        return selected;
    }

    public void setSelected(Right selected) {
        this.selected = selected;
    }

    public void add() {
        selected = new Right();
    }

    public void reset() {
        rightIdSearch = null;
        rightNameSearch = null;
        selected = null;
    }

    public void search() {

        Right o = new Right();
        o.setRightId(rightIdSearch);
        o.setRightName(rightNameSearch);
        resultSearch = dao.search(o);
    }

    public void view(Right right) {
        selected = right;
    }

    public boolean checkDup(String value) {
        Right item = new Right();
        item.setRightName(value);
        if (dao.search(item).size() == 0)
            return false;
        return true;
    }

    public void save() {
        FacesContext context = FacesContext.getCurrentInstance();

        if (selected.getRightId() != null)
            dao.update(selected);
        else {
            if (checkDup(selected.getRightName())) {
                RequestContext.getCurrentInstance().addCallbackParam(
                        "checkBusinessFail", true);
                context.addMessage("growl", new FacesMessage(
                        FacesMessage.SEVERITY_ERROR, "Add Error",
                        "Duplicate Name:" + selected.getRightName()));
                return;
            }
            dao.create(selected);
        }
        context.addMessage("growl", new FacesMessage("Operation Success",
                "Save ID: " + selected.getRightId()));
        search();

    }

    public void delete() {

        dao.delete(selected.getRightId());
        search();
        FacesContext.getCurrentInstance().addMessage(
                "growl",
                new FacesMessage("Operation Success", "Delete ID: "
                        + selected.getRightId())
        );
    }
}
